package com.giaothong.db.table;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.giaothong.model.entity.LocationSearchEntity;
import com.google.android.maps.GeoPoint;

public class DtbLikeLocation {

	// ===========================================================================
	// Database table & key
	// ===========================================================================

	public static final String TABLE_LIKE_LOCATION = "dtbLikeLocation";

	public static class DtbLikeLocationKey {
		public static final String ICON = "icon";
		public static final String NAME = "name";
		public static final String TYPE = "type";
		public static final String ADDRESS = "address";
		public static final String LONG = "long";
		public static final String LAT = "lat";
	}

	// ===========================================================================
	// Static functions for manager this table
	// ===========================================================================

	/**
	 * call this function when create table
	 * 
	 * @param db
	 */
	public static void createTable(SQLiteDatabase db) {
		String CREATE_TABLE_QUERY = "CREATE TABLE " + TABLE_LIKE_LOCATION + "(" + DtbLikeLocationKey.ADDRESS + " TEXT," + DtbLikeLocationKey.ICON + " TEXT,"
				+ DtbLikeLocationKey.NAME + " TEXT,"
				+ DtbLikeLocationKey.LAT + " INTEGER,"
				+ DtbLikeLocationKey.LONG + " INTEGER,"
				+ DtbLikeLocationKey.TYPE + " TEXT,"
				+ "PRIMARY KEY (" + DtbLikeLocationKey.LAT + "," + DtbLikeLocationKey.LONG
				+ "))";

		db.execSQL(CREATE_TABLE_QUERY);
	}

	/**
	 * call this function when want to drop this table
	 * 
	 * @param db
	 */
	public static void dropTable(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIKE_LOCATION);
	}


	/**
	 * Executable select query
	 * 
	 * @param db
	 * @param selectQuery
	 * @return
	 */
	public static ArrayList<LocationSearchEntity> querySelect(SQLiteDatabase db, String selectQuery) {
		ArrayList<LocationSearchEntity> locationSearchEntities = new ArrayList<LocationSearchEntity>();

		Cursor cursor = db.rawQuery(selectQuery, null);
		if (cursor.moveToFirst()) {
			do {
				LocationSearchEntity entity = new LocationSearchEntity();
				entity.setAddress(cursor.getString(cursor.getColumnIndex(DtbLikeLocationKey.ADDRESS)));
				entity.setIcon(cursor.getString(cursor.getColumnIndex(DtbLikeLocationKey.ICON)));
				entity.setName(cursor.getString(cursor.getColumnIndex(DtbLikeLocationKey.NAME)));
				entity.setTypes(cursor.getString(cursor.getColumnIndex(DtbLikeLocationKey.TYPE)));
				entity.setGeoPoint(new GeoPoint(cursor.getInt(cursor.getColumnIndex(DtbLikeLocationKey.LAT)), cursor.getInt(cursor.getColumnIndex(DtbLikeLocationKey.LONG))));

				locationSearchEntities.add(entity);
			} while (cursor.moveToNext());
		}

		return locationSearchEntities;
	}

	/**
	 * get Location from lat lon
	 * 
	 * @param db
	 * @param lat
	 * @param lon
	 * @return
	 */
	public static LocationSearchEntity getLikeLocationFromId(SQLiteDatabase db, int lat, int lon) {
		LocationSearchEntity locationSearchEntity = null;
		String selectQuery = "SELECT * FROM " + TABLE_LIKE_LOCATION + " WHERE " + DtbLikeLocationKey.LAT + " = " + lat + " AND " + DtbLikeLocationKey.LONG + " = " + lon;
		Cursor cursor = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			locationSearchEntity = new LocationSearchEntity();
			locationSearchEntity.setAddress(cursor.getString(cursor.getColumnIndex(DtbLikeLocationKey.ADDRESS)));
			locationSearchEntity.setGeoPoint(new GeoPoint(lat, lon));
			locationSearchEntity.setIcon(cursor.getString(cursor.getColumnIndex(DtbLikeLocationKey.ICON)));
			locationSearchEntity.setName(cursor.getString(cursor.getColumnIndex(DtbLikeLocationKey.NAME)));
			locationSearchEntity.setTypes(cursor.getString(cursor.getColumnIndex(DtbLikeLocationKey.TYPE)));
		}
		return locationSearchEntity;
	}




	/**
	 * delete All row from database
	 * 
	 * @param db
	 * @param
	 */
	public static int deleteAllRowFromDatabase(SQLiteDatabase db) {
		return db.delete(TABLE_LIKE_LOCATION, null, null);
	}

	/**
	 * insert new row
	 * 
	 * @param db
	 * @param entity
	 */
	public static void insertToDatabase(SQLiteDatabase db, LocationSearchEntity entity) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(DtbLikeLocationKey.ADDRESS, entity.getAddress());
		contentValues.put(DtbLikeLocationKey.ICON, entity.getIcon());
		contentValues.put(DtbLikeLocationKey.LAT, entity.getGeoPoint().getLatitudeE6());
		contentValues.put(DtbLikeLocationKey.LONG, entity.getGeoPoint().getLongitudeE6());
		contentValues.put(DtbLikeLocationKey.NAME, entity.getName());
		contentValues.put(DtbLikeLocationKey.TYPE, entity.getTypes());

		db.insert(TABLE_LIKE_LOCATION, null, contentValues);
	}

	/**
	 * update row in database
	 * 
	 * @param db
	 * @param entity
	 */
	public static void updateRowOfDatabase(SQLiteDatabase db, LocationSearchEntity entity) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(DtbLikeLocationKey.ADDRESS, entity.getAddress());
		contentValues.put(DtbLikeLocationKey.ICON, entity.getIcon());
		contentValues.put(DtbLikeLocationKey.NAME, entity.getName());
		contentValues.put(DtbLikeLocationKey.TYPE, entity.getTypes());

		db.update(TABLE_LIKE_LOCATION, contentValues, DtbLikeLocationKey.LAT + " = ? AND " + DtbLikeLocationKey.LONG + " = ? ", new String[] { entity.getGeoPoint().getLatitudeE6() + "", entity.getGeoPoint().getLongitudeE6() + "" });
	}

	/**
	 * delete row of database
	 * 
	 * @param db
	 * @param entity
	 */
	public static void deleteRowOfDatabase(SQLiteDatabase db, LocationSearchEntity entity) {
		db.delete(TABLE_LIKE_LOCATION, DtbLikeLocationKey.LAT + " = ? AND " + DtbLikeLocationKey.LONG + " = ? ", new String[] { entity.getGeoPoint().getLatitudeE6() + "", entity.getGeoPoint().getLongitudeE6() + "" });
	}


}

